#!/bin/bash

# Slackware build script for zork

# Written by B. Watson (yalhcru@gmail.com)

# Licensed under the WTFPL. See http://sam.zoy.org/wtfpl/ for details.

# VERSION is the last modification date of this script. The various zcode
# files of course have their own version information, but at this late date
# they aren't going to ever change.

PRGNAM=zork
VERSION=${VERSION:-20121202}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

ARCH=noarch

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
mkdir $PRGNAM-$VERSION
cd $PRGNAM-$VERSION

# zork 1/2/3 are z-code version 3 (z3), uu is z5.
mkdir -p $PKG/usr/share/zcode $PKG/usr/doc/$PRGNAM-$VERSION/invisiclues
for game in 1 2 3; do
  rm -rf *
  unzip -a -LL $CWD/zork$game.zip
  cat data/zork$game.dat > $PKG/usr/share/zcode/zork$game.z3
  cat readme.txt > $PKG/usr/doc/$PRGNAM-$VERSION/readme-zork$game.txt
done

rm -rf *
unzip -a -LL $CWD/ztuu.zip
cat ztuu.z5 > $PKG/usr/share/zcode/ztuu.z5
cat readme.txt > $PKG/usr/doc/$PRGNAM-$VERSION/readme-ztuu.txt
cat invisiclues.txt > $PKG/usr/doc/$PRGNAM-$VERSION/invisiclues/ztuu.txt
cat invisiclues.html > $PKG/usr/doc/$PRGNAM-$VERSION/invisiclues/ztuu.html

# ztuu has invisiclues in the zip file, add the clues for 1/2/3 also
cat $CWD/ZorkI.inv > $PKG/usr/doc/$PRGNAM-$VERSION/invisiclues/zork1.txt
cat $CWD/ZorkII.inv > $PKG/usr/doc/$PRGNAM-$VERSION/invisiclues/zork2.txt
cat $CWD/ZorkIII.inv > $PKG/usr/doc/$PRGNAM-$VERSION/invisiclues/zork3.txt

cat $CWD/README.interpreters > $PKG/usr/doc/$PRGNAM-$VERSION/README.interpreters
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

# Script wrapper and .desktop files written for this SlackBuild
mkdir -p \
  $PKG/usr/share/zork \
  $PKG/usr/bin \
  $PKG/usr/share/applications

install -m0755 $CWD/zork.sh $PKG/usr/share/zork/zorkwrapper.sh

for game in zork1 zork2 zork3 ztuu; do
  ln -s ../share/zork/zorkwrapper.sh $PKG/usr/bin/$game
  cat $CWD/desktop/$game.desktop > $PKG/usr/share/applications/$game.desktop
done

ln -s ../share/zork/zorkwrapper.sh $PKG/usr/bin/$PRGNAM

# Icon converted from Z.ICO here:
# http://www.ifarchive.org/if-archive/infocom/icons/ZorkLetters.zip
mkdir -p $PKG/usr/share/pixmaps
cat $CWD/$PRGNAM.png > $PKG/usr/share/pixmaps/$PRGNAM.png

mkdir -p $PKG/install
sed "s,@VERSION@,$VERSION," $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
